/*
   Copyright (c) 2006 Eric J. Feminella  <eric@ericfeminella.com>
   All rights reserved.

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is furnished
   to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in all
   copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
   PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

   @internal
 */

package com.ericfeminella.utils
{
	import mx.utils.StringUtil;

	/**
	 *
	 * All static utility class which builds on mx.utils.StringUtil to add
	 * additional methods for replacing items in a specified String
	 *
	 * @see mx.utils.StringUtil
	 *
	 */
	public final class StringUtils
	{
		/**
		 *
		 * Replaces a specific section of a String with a new String Object
		 *
		 * @param The source String Object to perform the operation on
		 * @param The section of the source String to be replaced
		 * @param The String Object whjich will replace the section
		 *
		 */
		public static function replace(source:String, search:String, substitution:String):String
		{
			var comp:Array=source.split(search);
			var result:String=comp.join(substitution);

			return result;
		}

		/**
		 *
		 * Replaces all instances of particular character(s) in a specific
		 * String with substitution character(s)
		 *
		 * @param The source String Object to perform the operation on
		 * @param The character in which all instances are to be replaced
		 * @param The character which will replace all instances
		 *
		 */
		public static function replaceCharacterInstances(source:String, character:String, substitution:String):String
		{
			var result:String="";

			for (var i:int=0; i < source.length; i++)
			{

				if (source.charAt(i) == character)
				{
					result+=substitution;
				}
				else
				{
					result+=source.charAt(i)
				}
			}
			return result;
		}

		/**
		 *
		 * Replaces all white space characters form the specified String
		 * Object and returns a new String without white space characters
		 *
		 * @param The string to trim whitespace
		 * @return The resulting trimmed String
		 *
		 */
		public static function trimWhiteSpace(source:String):String
		{
			var pattern:RegExp=/[\s]+/g;
			return source.replace(pattern, "");
		}

		/**
		 *
		 * Determines if the specified String contains alpha characters
		 *
		 * @param The string to evaluate
		 * @return true if the string contains alpha characters, otherwise false
		 *
		 */
		public static function containsAlpha(source:String):Boolean
		{
			var pattern:RegExp=/[^a-zA-Z]/;
			return source.search(pattern) != 0;
		}

		/**
		 *
		 * Determines if the specified String contains numeric characters
		 *
		 * @param The string to evaluate
		 * @return true if the string contains numeric characters, otherwise false
		 *
		 */
		public static function containsNumeric(source:String):Boolean
		{
			var pattern:RegExp=/[^1-90-9]/;
			return source.search(pattern) != 0;
		}

		/**
		 *
		 * Determines if the specified String contains alpha and / or numeric characters
		 *
		 * @param The string to evaluate
		 * @return true if the string contains alpha/numeric characters, otherwise false
		 *
		 */
		public static function containsAlphaNumeric(source:String):Boolean
		{
			var pattern:RegExp=/[^a-zA-Z0-9]/;
			return source.search(pattern) != 0;
		}

		/**
		 *
		 * Removes all xml / html tags and returns the a non-marked up String
		 *
		 * @param The string to remove html tags from
		 * @return the resulting non-marked up String
		 *
		 */
		public static function removeHTML(source:String):String
		{
			var pattern:RegExp=/<[^>]*>/g;
			return source.replace(pattern, "");
		}
	}
}